package com.lyn.qrcode;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.Hashtable;

/**
 * Zxing二维码管理器
 *
 * @author Lyn 2022/12/25
 * @version 1.0.0
 */
class ZxingManager {

    private final Hashtable<DecodeHintType, String> decodeHintHashtable = new Hashtable<>();
    private final Hashtable<EncodeHintType, String> encodeHintHashtable = new Hashtable<>();
    private final Reader reader;
    private final QRCodeWriter qrCodeWriter;

    public ZxingManager() {
        decodeHintHashtable.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        encodeHintHashtable.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        // 容错率设置
        encodeHintHashtable.put(EncodeHintType.ERROR_CORRECTION, "L");
        // 空白边距设置
        encodeHintHashtable.put(EncodeHintType.MARGIN, "2");
        reader = new MultiFormatReader();
        qrCodeWriter = new QRCodeWriter();
    }


    public Bitmap create(String qrCode, int width, int height) {
        return create(qrCode, Color.BLACK, Color.WHITE, width, height);
    }

    public Bitmap create(String qrCode, int blackColor, int whiteColor, int width, int height) {
        try {
            if (TextUtils.isEmpty(qrCode)) {
                return null;
            }
            BitMatrix bitMatrix = qrCodeWriter.encode(qrCode, BarcodeFormat.QR_CODE
                    , width, height, encodeHintHashtable);
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    //bitMatrix.get(x,y)方法返回true是黑色色块，false是白色色块
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = blackColor;
                    } else {
                        pixels[y * width + x] = whiteColor;
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    public String decode(Bitmap bitmap) {
        try {
            int[] intArray = new int[bitmap.getWidth() * bitmap.getHeight()];
            bitmap.getPixels(intArray, 0, bitmap.getWidth()
                    , 0, 0, bitmap.getWidth(), bitmap.getHeight());
            RGBLuminanceSource source = new RGBLuminanceSource(bitmap.getWidth()
                    , bitmap.getHeight(), intArray);
            BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
            return reader.decode(bitmap1, decodeHintHashtable).getText();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
}
